﻿Imports EstoqueBLL
Class FabricanteCON
    Dim objBLL As New FabricanteBLL
    Public tipo As String
    Public objfabri As New FabricanteBLL()
#Region "Cadastrar"
    Private Sub TSBCadastrar_Click(sender As System.Object, e As System.EventArgs) Handles TSBCadastrar.Click
        FabricanteCAD.ShowDialog()
        carregagrid()
    End Sub
#End Region

#Region "Carrega Grid"
    Public Sub carregagrid()
        Dim dt As New DataTable
        dt = objBLL.localizar("", 9)
        DGVDados.DataSource = dt
        DGVDados.Refresh()
    End Sub
#End Region

#Region "Combobox Filtro"
    Private Sub CBFiltro_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBFiltro.SelectedIndexChanged
        TXTLocalizar.Clear()
        If (CBFiltro.Text = "Status") Then
            TXTLocalizar.Visible = False
            CBStatus.Visible = True
            CBStatus.SelectedIndex = 0
            CBStatus_SelectedIndexChanged(Nothing, Nothing)
        Else
            TXTLocalizar.Clear()
            TXTLocalizar.Visible = True
            CBStatus.Visible = False
        End If
        If TXTLocalizar.Text = "" Then
            carregagrid()
        End If
    End Sub
#End Region

#Region "Combobox Status"
    Private Sub CBStatus_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBStatus.SelectedIndexChanged
        Dim sta As Integer = 0
        If (CBStatus.Text = "Ativo") Then sta = 1
        If (CBStatus.Text = "Inativo") Then sta = 2
        DGVDados.DataSource = objBLL.localizar(sta, 3)
        DGVDados.Refresh()
    End Sub
#End Region

#Region "Load"

    Private Sub FabricanteCON_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If (e.KeyCode = Keys.Enter) Then
            SendKeys.Send("{TAB}")
        End If
        If (e.KeyCode = Keys.Escape) Then
            Me.Close()
        End If
    End Sub

    Private Sub FabricanteCON_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        carregagrid()
        CBFiltro.SelectedIndex = 0
        CBFiltro.Focus()
    End Sub
#End Region

#Region "TextBox Localizar"
    Private Sub TXTLocalizar_TextChanged(sender As System.Object, e As System.EventArgs) Handles TXTLocalizar.TextChanged
        If (CBFiltro.Text = "Descrição") Then
            DGVDados.DataSource = objBLL.localizar(TXTLocalizar.Text, 2)
        End If
    End Sub
#End Region

#Region "Double click do datagrid"
    Private Sub DGVDados_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVDados.CellDoubleClick
        Try

            If tipo = "ret" Then
                Dim indice As Integer = -1

                'Percorre O DataGridView Verificando a Linha selecionada
                For Each linha As DataGridViewRow In DGVDados.SelectedRows
                    If (Not linha.Cells("fab_codigo") Is Nothing) Then
                        'Guarda A Posição Selecionada
                        indice = linha.Index
                    End If
                Next
                If (indice <> -1) Then
                    If (DGVDados.Rows(indice).Cells("sta_codigo").Value = 1) Then
                        objfabri = New FabricanteBLL(Val(DGVDados.Rows(indice).Cells("fab_codigo").Value.ToString()))
                        Me.Close()
                    Else
                        Throw New Exception("Fabricante inativo")
                    End If
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
#End Region

#Region "Voltar"
    Private Sub TSBVoltar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        Me.Close()
    End Sub
#End Region

#Region "Cor de fundo do datagridview"
    Private Sub DGVDados_RowPrePaint(sender As System.Object, e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DGVDados.RowPrePaint
        If DGVDados.Rows(e.RowIndex).Index Mod (2) = 1 Then
            DGVDados.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Gainsboro
        End If
    End Sub
#End Region

#Region "DatagridView Clicar com enter"
    Private Sub DGVDado_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles DGVDados.KeyPress
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
            DGVDados_CellDoubleClick(Nothing, Nothing)
        End If
    End Sub
#End Region
End Class